

import requests
import time
import hashlib
import random

def main():
    world = input('请输入要翻译的词：')
    url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
        "Referer": "http://fanyi.youdao.com/",
        "Cookie": 'OUTFOX_SEARCH_USER_ID_NCOO=136521905.95429793; _ga=GA1.2.395349781.1590806241; OUTFOX_SEARCH_USER_ID="-1025807983@10.169.0.82"; JSESSIONID=aaap9p-iS0lup6l3azfmx; ___rl__test__cookies=1593523333872'
    }
    salt = str(time.time()*1000) + str(random.randint(0,10))
    temp = "fanyideskweb" + world + salt + "mmbP%A-r6U3Nw(n]BjuEU"
    sign = hashlib.md5(temp.encode('utf-8')).hexdigest()
    ts = time.time()*1000
    data = {
        "i": world,
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": salt,
        "sign": sign,
        "ts": ts,
        "bv": "02a6ad4308a3443b3732d855273259bf",
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_REALTlME"
    }
    response = requests.post(url,headers=headers,data=data)
    print(response.json()['translateResult'][0][0]['tgt'])


if __name__ == '__main__':
    main()